﻿using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;

namespace BackEndFunctions
{
    class Program
    {
        static void Main(string[] args)
        {
            string filename = @"C:\Users\16528\Desktop\test.txt";
            FileStream SourceStream = File.Open(filename, FileMode.Open);
            byte[] test;
            List<Type> inputType = new List<Type>
            {
                typeof(bool),
                typeof(byte),
                typeof(double),
                typeof(float),
                typeof(int),
                typeof(long),
                typeof(sbyte),
                typeof(short),
                typeof(string),
                typeof(uint),
                typeof(ulong),
                typeof(ushort)
            };
            test = new byte[SourceStream.Length];
            SourceStream.Read(test, 0, (int)SourceStream.Length);
            //Console.WriteLine(result[0]);
            //for (int i = 0; i < SourceStream.Length; i++)
            //{
            //    Console.Write((char)result[i]);
            //}
            //List<DataUnit> output = VariableDivision(test, inputType, ',', '/');
            //foreach (var s in output)
            //{
            //    //Console.WriteLine($"{s.caption}, value = {s.value}");
            //}
            //SourceStream.Close();
            //FixedDivision(test, inputType, "AA05AFF1");
        }

        /// <summary>
        /// 定界符分割
        /// </summary>
        /// <param name="input"> 输入数据流 </param>
        /// <param name="dataType"> 类型列表 </param>
        /// <param name="dataDelimiter"> 数据定界符 </param>
        /// <param name="frameDelimiter"> 帧定界符 </param>
        /// <returns> 分割后的数据列表 </returns>
        static List<DataUnit> VariableDivision(byte[] input, List<Type> dataType, char dataDelimiter, char frameDelimiter)
        {
            bool frameFlag = false;
            List<DataUnit> feedBack = new List<DataUnit>();
            string temp = string.Empty;
            int dataCount = 0;
            foreach (var item in input)
            {
                if ((char)item == frameDelimiter && frameFlag == false)
                {
                    frameFlag = true;
                    continue;
                }
                if (frameFlag == true)
                {
                    if ((char)item != dataDelimiter && (char)item != frameDelimiter)
                    {
                        temp = $"{temp}{(char)item}";
                    }
                    else
                    {
                        if(dataType[dataCount] == typeof(int))
                        {
                            IntUnit dataUnit = new IntUnit();
                            dataUnit.dataType = typeof(int);
                            dataUnit.value = int.Parse(temp);
                            dataUnit.caption = $"{dataCount.ToString()}: int";
                            feedBack.Add(dataUnit);
                        }
                        if (dataType[dataCount] == typeof(long))
                        {
                            LongUnit dataUnit = new LongUnit();
                            dataUnit.dataType = typeof(long);
                            dataUnit.value = long.Parse(temp);
                            dataUnit.caption = $"{dataCount.ToString()}: long";
                            feedBack.Add(dataUnit);
                        }
                        if (dataType[dataCount] == typeof(short))
                        {
                            ShortUnit dataUnit = new ShortUnit();
                            dataUnit.dataType = typeof(short);
                            dataUnit.value = short.Parse(temp);
                            dataUnit.caption = $"{dataCount.ToString()}: short";
                            feedBack.Add(dataUnit);
                        }
                        if (dataType[dataCount] == typeof(float))
                        {
                            FloatUnit dataUnit = new FloatUnit();
                            dataUnit.dataType = typeof(float);
                            dataUnit.value = float.Parse(temp);
                            dataUnit.caption = $"{dataCount.ToString()}: float";
                            feedBack.Add(dataUnit);
                        }
                        if (dataType[dataCount] == typeof(double))
                        {
                            DoubleUnit dataUnit = new DoubleUnit();
                            dataUnit.dataType = typeof(double);
                            dataUnit.value = double.Parse(temp);
                            dataUnit.caption = $"{dataCount.ToString()}: double";
                            feedBack.Add(dataUnit);
                        }
                        if (dataType[dataCount] == typeof(uint))
                        {
                            UintUnit dataUnit = new UintUnit();
                            dataUnit.dataType = typeof(uint);
                            dataUnit.value = uint.Parse(temp);
                            dataUnit.caption = $"{dataCount.ToString()}: uint";
                            feedBack.Add(dataUnit);
                        }
                        if (dataType[dataCount] == typeof(ulong))
                        {
                            UlongUnit dataUnit = new UlongUnit();
                            dataUnit.dataType = typeof(ulong);
                            dataUnit.value = ulong.Parse(temp);
                            dataUnit.caption = $"{dataCount.ToString()}: ulong";
                            feedBack.Add(dataUnit);
                        }
                        if (dataType[dataCount] == typeof(ushort))
                        {
                            UshortUnit dataUnit = new UshortUnit();
                            dataUnit.dataType = typeof(ushort);
                            dataUnit.value = ushort.Parse(temp);
                            dataUnit.caption = $"{dataCount.ToString()}: ushort";
                            feedBack.Add(dataUnit);
                        }
                        if (dataType[dataCount] == typeof(bool))
                        {
                            BoolUnit dataUnit = new BoolUnit();
                            dataUnit.dataType = typeof(bool);
                            dataUnit.value = bool.Parse(temp);
                            dataUnit.caption = $"{dataCount.ToString()}: bool";
                            feedBack.Add(dataUnit);
                        }
                        if (dataType[dataCount] == typeof(byte))
                        {
                            ByteUnit dataUnit = new ByteUnit();
                            dataUnit.dataType = typeof(byte);
                            dataUnit.value = byte.Parse(temp);
                            dataUnit.caption = $"{dataCount.ToString()}: byte";
                            feedBack.Add(dataUnit);
                        }
                        if (dataType[dataCount] == typeof(sbyte))
                        {
                            SbyteUnit dataUnit = new SbyteUnit();
                            dataUnit.dataType = typeof(sbyte);
                            dataUnit.value = sbyte.Parse(temp);
                            dataUnit.caption = $"{dataCount.ToString()}: sbyte";
                            feedBack.Add(dataUnit);
                        }
                        if (dataType[dataCount] == typeof(string))
                        {
                            StringUnit dataUnit = new StringUnit();
                            dataUnit.dataType = typeof(string);
                            dataUnit.value = temp;
                            dataUnit.caption = $"{dataCount.ToString()}: string";
                            feedBack.Add(dataUnit);
                        }
                        temp = string.Empty;
                        dataCount++;
                    }
                    if ((char)item == frameDelimiter)
                    {
                        return feedBack;
                    }
                }
            }
            //TODO 改为抛出异常
            feedBack.Clear();
            return feedBack;
        }
        //static List<DataUnit> FixedDivision(byte[] input, List<Type> dataType, string headerHex)
        //{
        //    List<DataUnit> feedBack = new List<DataUnit>();
        //    int headerLengh = headerHex.Length/2;
        //    byte[] header = new byte[headerLengh];
        //    for (int i = 0; i < headerLengh; i++)
        //    {
        //        header[i] = (byte)(((headerHex[2 * i] >= 'A') ? (headerHex[2 * i] - 55) : (headerHex[2 * i] - 48)) * 16 + ((headerHex[2 * i + 1] >= 'A') ? (headerHex[2 * i + 1] - 55) : (headerHex[2 * i + 1] - 48)));
        //    }
        //    foreach (var item in input)
        //    {

        //    }
        //}
    }
}
